package exa.unicen.edu.ringo.rmodel.builder;

import java.util.Hashtable;

import com.thoughtworks.xstream.XStream;

import exa.unicen.edu.ringo.rmodel.bean.Configuration;
import exa.unicen.edu.ringo.rmodel.bean.IBean;
import exa.unicen.edu.ringo.rmodel.bean.Node;
import exa.unicen.edu.ringo.rmodel.bean.Service;
import exa.unicen.edu.ringo.rmodel.builder.factory.ModelFactory;
import exa.unicen.edu.ringo.rmodel.builder.xstream.ConfigurationXStream;


public class ModelBuilder {

	public static ModelBuilder instance = new ModelBuilder();
	
	private  Hashtable<String,ModelFactory> factories;
	
	private ModelBuilder(){
		factories = new Hashtable<String,ModelFactory>();
		
		factories.put(Configuration.class.getName(), new ModelFactory<Configuration>(ConfigurationXStream.instance));
		factories.put(Node.class.getName(), 		 new ModelFactory<Node>(ConfigurationXStream.instance));
		factories.put(Service.class.getName(), 		 new ModelFactory<Service>(ConfigurationXStream.instance));
	}
	
	public IBean marshal(Class clazz, String xml){
		return (IBean) factories.get(clazz.getName()).fromXML(xml);
	}
	
	public String unmarshal(Class clazz, IBean model){
		return factories.get(clazz.getName()).toXML(model);
	}
		
}
